package com.ciwong.epaper.widget.wheel.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.SchoolDetails;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:实名登录学校页面适配器
 * @author: wyk
 * @time: 2021/8/4 18:39
 */
public class SchoolListAdapter extends BaseAdapter {
    private Context context;

    private List<SchoolDetails> totalSchoolDetails;
    private LayoutInflater inflater;
    final int VIEW_TYPE = 3;
    private Map<String, Integer> alphaIndexer = new HashMap<>();// 存放存在的汉语拼音首字母和与之对应的列表位置

    public  SchoolListAdapter(Context context,
                              List<SchoolDetails> totalSchoolDetails, Map<String, Integer> alphaIndexers) {
        this.context = context;
        this.totalSchoolDetails = totalSchoolDetails;
        this.alphaIndexer = alphaIndexers;
        inflater = LayoutInflater.from(context);

        alphaIndexer.clear();
        if (null != totalSchoolDetails && totalSchoolDetails.size() > 0 ){
            for (int i = 0; i < totalSchoolDetails.size(); i++) {
                // 当前汉语拼音首字母
                String currentStr = totalSchoolDetails.get(i).getKey();

                String previewStr = (i - 1) >= 0 ? totalSchoolDetails.get(i - 1).getKey() : " ";
                if (!previewStr.equals(currentStr)) {
                    String name = currentStr;
                    alphaIndexer.put(name, i);
                }
            }
        }
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE;
    }

    @Override
    public int getItemViewType(int position) {
        return position < 2 ? position : 2;
    }

    @Override
    public int getCount() {
        return totalSchoolDetails == null ? 0 : totalSchoolDetails.size();
    }

    @Override
    public Object getItem(int position) {
        return totalSchoolDetails.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final TextView curCityNameTv;
        ViewHolder holder;
        int viewType = getItemViewType(position);
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.activity_school_list_item, null);
            holder.cityNameTv = convertView.findViewById(R.id.city_name_tv);
            holder.city_key_tv = convertView.findViewById(R.id.city_key_tv);
            holder.area_text = convertView.findViewById(R.id.area_text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        SchoolDetails cityEntity = totalSchoolDetails.get(position);
        holder.city_key_tv.setVisibility(View.VISIBLE);
        holder.city_key_tv.setText(cityEntity.getKey());
        holder.cityNameTv.setText(cityEntity.getSchoolName());
        if (null != cityEntity.getAreaName() && !"".equals(cityEntity.getAreaName())){
            holder.area_text.setVisibility(View.VISIBLE);
            holder.area_text.setText("("+cityEntity.getAreaName()+")");
        }else {
            holder.area_text.setVisibility(View.GONE);
        }

        if (position >= 1) {
            SchoolDetails preCity = totalSchoolDetails.get(position - 1);
            if (preCity.getKey().equals(cityEntity.getKey())) {
                holder.city_key_tv.setVisibility(View.GONE);
            } else {
                holder.city_key_tv.setVisibility(View.VISIBLE);
            }
        }


        return convertView;
    }

    private class ViewHolder {
       private TextView cityNameTv;
       private TextView city_key_tv;
       private TextView area_text;
    }
    
    public void setData(List<SchoolDetails> schoolDetails){
        this.totalSchoolDetails = schoolDetails;
        alphaIndexer.clear();
        if (null != totalSchoolDetails && totalSchoolDetails.size() > 0 ){
            for (int i = 0; i < totalSchoolDetails.size(); i++) {
                // 当前汉语拼音首字母
                String currentStr = totalSchoolDetails.get(i).getKey();

                String previewStr = (i - 1) >= 0 ? totalSchoolDetails.get(i - 1).getKey() : " ";
                if (!previewStr.equals(currentStr)) {
                    String name = currentStr;
                    alphaIndexer.put(name, i);
                }
            }
        }
        this.notifyDataSetChanged();
    }
}


